<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>黑马面面</title>
    <meta name="description" content="黑马面面">
    <meta name="keywords" content="黑马面面">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../css/element-ui.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>

<body class="hold-transition">

    <div id="app">
        <div class="content-header common-search-border1">
            <el-button class="butAdd" @click="handleCreate"><i class="fa fa-plus" aria-hidden="true"></i> 新增用户
            </el-button>
        </div>
        <div class="app-container">
            <div class="box common-search-border2">
                <div class="filter-container">
                    <!-- 搜索栏 -->
                    <el-form :inline="true" :model="requestParameters" ref="requestParameters" class="formInfo">
                        <el-form-item label="用户名称" label-width="80px">
                            <el-input placeholder="请输入" v-model="requestParameters.userName" style="width: 200px;"
                                class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        </el-form-item>
                        <el-form-item label="状态" label-width="40px">
                            <el-select placeholder="请选择" v-model="requestParameters.isDis">
                                <el-option :key="item.id" :label="item.value" :value="item.id"
                                    v-for="item in baseData.isDis"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button @click="handleRest">清除</el-button>
                            <el-button type="primary" class="butOK" @click="handleSearch">搜索</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="box common-table-border" style="margin:8px 0 0;">
                <el-table :key="tableKey" :data="items" fit highlight-current-row style="width: 100%;">
                    <el-table-column label="序号" width="50px" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.$index+1 }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="用户名称" align="center">
                        <template slot-scope="scope">
                            <span class="link-type">{{ scope.row.username }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="邮箱" align="center">
                        <template slot-scope="scope">
                            <span class="link-type">{{ scope.row.email }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建日期" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.createDate }}
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.row.remark }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.row.state == 0 ? '启用' : '禁用' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180px">
                        <template slot-scope="scope">
                            <div class="operation" v-if="scope.row.username != 'admin'">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)"><i
                                        class="fa fa-pencil-square-o" aria-hidden="true"></i> 修改
                                </el-button>
                                <!--<el-button type="primary" size="mini" @click="handleDisable(scope.row)"><i class="fa fa-exclamation-circle" aria-hidden="true"></i> {{ scope.row.isDis == 0 ? '禁用' : '启用' }}</el-button>-->
                                <el-button size="mini" type="danger" @click="handleDeleted(scope.row)"><i
                                        class="fa fa-trash-o" aria-hidden="true"></i> 删除
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination-container">
                    <el-pagination class="pagiantion" v-show="pagination.total>0" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" :current-page="pagination.pageNum"
                        :page-sizes="[10, 20, 50, 100]" :page-size="pagination.pageSize"
                        layout="total, sizes, prev, pager, next, jumper" :total="pagination.total">
                    </el-pagination>
                </div>
            </div>
            <!-- 新增用户弹层 -->
            <el-dialog title="新增用户" align="center" :visible.sync="dialogFormVisible" width="40%" top="8vh">
                <el-form label-width="120px" :model="form" :rules="rules" ref="form">
                    <el-form-item label="用户名称：" prop="username">
                        <el-input v-model="form.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱：" prop="email">
                        <el-input v-model="form.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码：" prop="password">
                        <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="重复密码：" prop="rePwd">
                        <el-input type="password" v-model="form.rePwd" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注：" prop="remark">
                        <el-input v-model="form.remark" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="handleCreateCancel">取 消</el-button>
                    <el-button type="primary" @click="handleCreateConfirm">确 定</el-button>
                </div>
            </el-dialog>
            <!-- end -->
            <!-- 修改用户弹层 -->
            <el-dialog title="修改用户" align="center" :visible.sync="dialogUptFormVisible" width="40%" top="8vh">
                <el-form label-width="120px" :model="uptForm" :rules="uptRules" ref="uptForm">
                    <el-form-item label="用户名称：" prop="username">
                        <el-input v-model="uptForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱：" prop="email">
                        <el-input v-model="uptForm.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注：" prop="remark">
                        <el-input v-model="uptForm.remark" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="handleUpdateCancel">取 消</el-button>
                    <el-button type="primary" @click="handleUpdateConfirm">确 定</el-button>
                </div>
            </el-dialog>
            <!-- end -->
        </div>

    </div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../js/element-ui.js"></script>
<script src="../plugins/jquery/dist/jquery.js"></script>
<script src="../js/data.json"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/base.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            //密码校验规则
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.password !== '') {
                        this.$refs.form.validateField('rePwd');
                    }
                    callback();
                }
            };
            //重复密码校验规则
            var validateCheckPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            //密码校验规则
            var uptValidatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.uptForm.password !== '') {
                        this.$refs.uptForm.validateField('rePwd');
                    }
                    callback();
                }
            };
            //重复密码校验规则
            var uptValidateCheckPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.uptForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                tableKey: 0,
                pagination: {
                    total: 0,
                    pageNum: 1,
                    pageSize: 10
                },
                loading: false,
                items: [],
                baseData: base,
                requestParameters: {
                    userName: '',
                    isDis: ''
                },
                dialogFormVisible: false,
                dialogUptFormVisible: false,
                form: {
                    username: '',
                    email: '',
                    password: '',
                    rePwd: '',
                    remark: ''
                },
                uptForm: {
                    userId: '',
                    username: '',
                    email: '',
                    remark: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请填写', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请填写', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    rePwd: [
                        { validator: validateCheckPass, trigger: 'blur' }
                    ]
                },
                uptRules: {
                    username: [
                        { required: true, message: '请填写', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请填写', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ]
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                var t = this;
                // 必传参数
                let params = {
                    currentPage: t.pagination.pageNum,
                    pageSize: t.pagination.pageSize,
                    queryParams: {}
                };
                // 选传参数
                let userName = t.requestParameters.userName;
                if (userName) {
                    params.queryParams.userName = userName;
                }
                let isDis = t.requestParameters.isDis;
                if (isDis !== '') {
                    params.queryParams.isDis = isDis;
                    
                }
                // 发送请求获取数据
                axios.post(app_path+"/user/findListAll.do",params)
                .then((response)=>{
                if(response.data.flag){
                t.pagination.total = response.data.result.total;
                t.items = response.data.result.rows;
                t.loading = true;
      }else{
          this.$message.error(response.data.message);
        }
        });
            },
            //Enter键查询
            handleFilter() {
                this.getList()
            },
            //清除
            handleRest() {
                this.requestParameters = {
                    userName: '',
                    isDis: ''
                }
            },
            //搜索
            handleSearch() {
                this.getList();
            },
            handleSizeChange(val) {
                this.pagination.pageSize = val;
                this.pagination.pageNum = 1;
                this.getList();
            },
            handleCurrentChange(val) {
                this.pagination.pageNum = val;
                this.getList();
            },
            // 新增用户
            handleCreate() {
                if (this.$refs['form']) {
                    this.$refs['form'].resetFields();
                }
                this.dialogFormVisible = true;
            },
           //新增用户确定
handleCreateConfirm() {
    var t = this;
    this.$refs['form'].validate((valid) => {
        if (valid) {
            let params = this.form;
            console.log("用户添加请求参数：");
            console.log(params);
            // 发送请求
            //this.$message.success("添加成功");
            //this.dialogFormVisible = false;
            //this.getList();
            // 发送请求
            axios.post(app_path + "/user/add.do", params).then((response) => {
                if (response.data.flag) {
                    this.$message.success("添加成功");
                    this.dialogFormVisible = false;
                } else {
                    this.$message.error(response.data.message);
                }
            })
            
            
        }
    });
    this.getList();
},
            //新增用户取消
            handleCreateCancel() {
                this.dialogFormVisible = false;
            },
            // 禁用
            handleDisable(row) {
                console.log(row);
                var t = this;


            },
            // 修改用户
       handleUpdate(row) {
     var t = this;
     if (t.$refs['uptForm']) {
        t.$refs['uptForm'].resetFields();
    }

    //发送异步请求根据id查询用户数据更新到页面上
    axios.get(app_path + "/user/findById.do?id=" + row.id).then((response) => {
        if (response.data.flag) {
            this.uptForm = response.data.result;
            this.dialogUptFormVisible = true; //显示修改用户对话框
        } else {
            this.$message.error(response.data.message);
        }
    })


},
            //修改用户确定
            handleUpdateConfirm() {
        
           var t = this;
         this.$refs['uptForm'].validate((valid) => {
         if (valid) {
            let params = this.uptForm;
            console.log("用户修改请求参数：");
            console.log(params);
            // 发送请求
            //this.$message.success("添加成功");
            //this.dialogFormVisible = false;
            //this.getList();
            // 发送请求
            axios.post(app_path + "/user/update.do", params).then((response) => {
                if (response.data.flag) {
                    this.$message.success(response.data.message);
                    this.dialogUptFormVisible = false;
                } else {
                    this.$message.error(response.data.message);
                }
            }).finally(() => {
                this.getList();
            });
        }
    });
},
                
            
            //修改用户取消
            handleUpdateCancel() {
                this.dialogUptFormVisible = false;
            },
            // 删除
                
            handleDeleted(row) {
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				//点击了确定进入的函数
				//获取删除的用户id
				let userId = row.id;
				//alert(userId);

				//发送异步请求进行删除操作
				axios.get(app_path+`/user/delete.do?userId=${userId}`)
					.then(result=>{

					if(result.data.flag){
						this.$message.success(result.data.message);
						this.getList();
					}else{
						this.$message.error(result.data.message);
					}
				})

			}).catch(() => {
				//点击了取消进入的函数
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
						}
					}
               
            })
        
    
</script>

</html>